package com.buddy.qreport.template.config;

import com.buddy.qreport.template.IReportTemplate;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * 当容器启动后，进行模板类扫描，自动分类管理
 */
@Component
public class ReportTemplateConfig implements BeanFactoryPostProcessor, ApplicationContextAware {

    private ApplicationContext applicationContext;


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        this.applicationContext = applicationContext;
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

        TemplateScanner scanner = new TemplateScanner((BeanDefinitionRegistry) configurableListableBeanFactory);

        scanner.setResourceLoader(applicationContext);

        scanner.scan(IReportTemplate.class.getPackage().getName());
    }
}
